﻿(function ($, connection, window) {
  var taskHub = connection.taskHub;
  var username = "andyjmay";

  var tasksViewModel = {
    tasks: ko.observableArray(),
    EditTask: function () {
      taskViewModel.TaskID(this.TaskID);
      taskViewModel.Title(this.Title);
      taskViewModel.AssignedTo(this.AssignedTo);
      taskViewModel.Status(this.Status);
      taskViewModel.Details(this.Details);
      //taskViewModel.DateCreated(this.DateCreated);
      
      //TODO: Fix this UI call
      $('#editTaskModal').modal('show');
    }
  };

  var taskViewModel = {
    TaskID: new ko.observable(),
    Title: new ko.observable(),
    AssignedTo: new ko.observable(),
    Status: new ko.observable(),
    Details: new ko.observable(),
    DateCreated: new ko.observable(),
    AddTask: function () {
      tasks.addTask(ko.toJSON(this));
    },
    UpdateTask: function () {
      tasks.updateTask(ko.toJSON(this));
    },
    DeleteTask: function () {
      tasks.deleteTask(ko.toJSON(this));
    }
  };

  taskHub.GotTasksForUser = function (results) {
    if (results) {
      $.each(results, function () {
        var task = this;
        tasksViewModel.tasks.push(task);
      });
    }
  };

  taskHub.GotLogMessage = function (logMessage) {
  };

  taskHub.AddedTask = function (task) {
    tasksViewModel.tasks.push(task);
  };

  taskHub.UpdatedTask = function (task) {
    taskHub.DeletedTask(task);
    tasksViewModel.tasks.push(task);
    tasksViewModel.tasks.sort(function (left, right) {
      return left === right ? 0 : (left.TaskID < right.TaskID ? -1 : 1)
    });
    //var tasksInViewModel = tasksViewModel.tasks();
    //for (var i = 0; i < tasksInViewModel.length; i++) {
    //  if (taskViewModel[i].TaskID == task.TaskID) {
    //    tasksViewModel.tasks().
    //  }
    //}
  };

  taskHub.DeletedTask = function (task) {
    tasksViewModel.tasks.remove(function (taskToRemove) {
      return taskToRemove.TaskID === task.TaskID;
    });
  };

  taskHub.HandleException = function (ex) {
    alert(ex);
  };

  // Connect to the hub
  $(function () {
    connection.hub.start(function () {
      taskHub.login(username)
               .fail(function (e) {
                 taskHub.HandleException(e);
               })
               .done(function (success) {
                 if (success === false) {
                   alert("failed");
                 } else {
                 }
               });
    });
  });

  var tasks = {
    addTask: function (newTask) {
      taskHub.addTask(newTask)
               .fail(function (e) {
                 taskHub.HandleException(e);
               })
               .done(function (success) {
                 if (success === false) {
                   alert("failed");
                 } else {                   
                 }
               });
    },
    updateTask: function (updatedTask) {
      taskHub.updateTask(updatedTask)
               .fail(function (e) {
                 taskHub.HandleException(e);
               })
               .done(function (success) {
                 if (success === false) {
                   alert("failed");
                 } else {
                 }
               });
    },
    deleteTask: function (taskToDelete) {
      taskHub.deleteTask(taskToDelete)
               .fail(function (e) {
                 taskHub.HandleException(e);
               })
               .done(function (success) {
                 if (success === false) {
                   alert("failed");
                 } else {
                 }
               });
    }
  };

  window.tasks = tasks;

  // Apply KO bindings
  ko.applyBindings(tasksViewModel, document.getElementById('tasks'));
  ko.applyBindings(taskViewModel, document.getElementById('newTaskModal'));
  ko.applyBindings(taskViewModel, document.getElementById('editTaskModal'));
})(jQuery, $.connection, window);